package com.example.administrator.yuejian23;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;


import com.example.administrator.yuejian23.Fragment.BaseFragment;
import com.example.administrator.yuejian23.Fragment.CenterFragment;
import com.example.administrator.yuejian23.Fragment.ClubFragment;
import com.example.administrator.yuejian23.Fragment.MyFragment;
import com.example.administrator.yuejian23.Fragment.ShowFragment;
import com.example.administrator.yuejian23.Fragment.TrainFragment;


/**
 * A simple {@link Fragment} subclass.
 */
public class MainFragment extends BaseFragment {

    public TextView titleView;
    private RadioGroup radioGroup;
    public Button anniu;

    private ClubFragment clubFragment;
    private TrainFragment trainFragment;
    private CenterFragment centerFragment;
    private ShowFragment showFragment;
    private MyFragment myFragment;

    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;




    public MainFragment() {
        // Required empty public constructor
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
       titleView = (TextView) view.findViewById(R.id.textView_title);
       radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);
     /*   anniu= (Button) view.findViewById(R.id.gongneng);*/
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case  R.id.radio_club:

                        titleView.setText("俱乐部列表");
                        /*anniu.setText("");*/
                        clubFragment = new ClubFragment();
                        replaceFragment(clubFragment);


                        break;
                    case  R.id.radio_train:
                        titleView.setText("培训");
                       /* anniu.setText("");*/
                        trainFragment = new TrainFragment();
                        replaceFragment(trainFragment);


                        break;
                    case  R.id.radio_center:
                        titleView.setText("主页");
                       /* anniu.setText("搜索");*/
                        centerFragment = new CenterFragment();
                        replaceFragment(centerFragment);


                        break;
                    case  R.id.radio_show:
                        titleView.setText("晒图");
                       /* anniu.setText("发布晒图");*/
                        showFragment = new ShowFragment();
                        replaceFragment(showFragment);


                        break;
                    case  R.id.radio_setting:
                        titleView.setText("我的");
                     /*   anniu.setText("");*/
                        myFragment = new MyFragment();
                        replaceFragment(myFragment);


                        break;
                }
            }
        });

        radioGroup.check(R.id.radio_club);

    }
    private  void replaceFragment(Fragment fragment){

        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragment_holder,fragment);
        fragmentTransaction.commit();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //在fragment中添加fragment时，调用getChildFragmentManager获取fragmentmanger
        fragmentManager = getChildFragmentManager();
        return inflater.inflate(R.layout.fragment_main, container, false);
    }

}
